package deng.testdb;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import org.apache.commons.lang.builder.ToStringBuilder;

public class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	private void run() {
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("HBCFGXML");
		EntityManager em = null;
		EntityTransaction tx = null;
		try {
			em = emf.createEntityManager();
			tx = em.getTransaction();
			tx.begin();
			runExample(em);
			tx.commit();
		} catch (Exception e) { 
			if (tx != null && tx.isActive()) 
				tx.rollback();
		} finally {
			if (em != null)
				em.close();
		}
	}

	private void runExample(EntityManager em) {
		Long count = (Long)em.createQuery("select count(e) from Employee e").getSingleResult();
		System.out.println(count);
		
		List<?> ls = em.createQuery("from Employee").setMaxResults(100).getResultList();
		for (Object object : ls) {
			System.out.println(ToStringBuilder.reflectionToString(object));
		}
	}
}
